env.setErrorMessageBoxEnabled(false)

local e = {}

DCSDBMENU = {}
SUPPORTMENU = {}

local function callSupport(ucid, pts)
    local url = "http://localhost:8079/api/player/callsupport"
    local params = "ucid=" .. ucid .. "&pts=" .. tostring(pts)
    return SendRequest(url, params)
end

local function responseHandle(ucid, supportTypeName, supportTypeId, groupSpawn, unitId, playerName)
    local ptsChange
    if supportTypeId == 1 then -- 反辐射
        ptsChange = 1000
    end

    if groupSpawn:GetFirstAliveGroup() ~= nil then
        trigger.action.outTextForUnit(unitId, "空域还有友方呼叫的支援, 无法呼叫", 10, false)
    else
        local response = callSupport(ucid, ptsChange)
        if response ~= "你的点数不够!!!" then -- 如果没有系统错误并且分数足够
            MESSAGE:New("玩家" .. playerName .. "使用积分呼叫了" .. supportTypeName .. "支援!", 10, "战争系统",
                false):ToAll()
            groupSpawn:Spawn()
            SOUND_AGENTJOINTEAM_01:ToAll()
        end
        trigger.action.outTextForUnit(unitId, response, 10, false)
    end
end

function e:onEvent(event)
    if event.id == 15 then -- birth
        if event.initiator:getCategory() == 1 then
            if event.initiator:getCoalition() == 2 and event.initiator:getPlayerName() ~= nil then
                local unit = event.initiator
                local playerName = unit:getPlayerName()
                local groupID


                if unit:getGroup() then
                    groupID = unit:getGroup():getID()
                else
                    return
                end

                if DCSDBMENU[tostring(groupID)] then
                    missionCommands.removeItemForGroup(groupID, DCSDBMENU[tostring(groupID)]) --清理菜单
                end

                DCSDBMENU[tostring(groupID)] = missionCommands.addSubMenuForGroup(groupID, "* 战争系统 *", nil) -- 主菜单

                missionCommands.addCommandForGroup(groupID, "- 查看战绩", DCSDBMENU[tostring(groupID)], -- 查看战绩的选项
                    function()
                        local url = "http://localhost:8079/api/player/myInfo"
                        local ucid = NET:New():GetPlayerUCID(nil, playerName)
                        if ucid then
                            local params = "ucid=" .. ucid
                            trigger.action.outTextForUnit(unit:getID(), SendRequest(url, params), 10, true)
                        else
                            trigger.action.outTextForUnit(unit:getID(), "系统错误,请联系管理员。UCID为空", 10, true)
                        end
                    end, {})

                -- => SUPPOTMENU
                SUPPORTMENU[tostring(groupID)] = missionCommands.addSubMenuForGroup(groupID, "* 呼叫支援 *",
                    DCSDBMENU[tostring(groupID)])

                missionCommands.addCommandForGroup(groupID, "- 呼叫反辐射支援(1000 pts)",
                    SUPPORTMENU[tostring(groupID)],                                                                -- 呼叫反辐射
                    function()
                        local ucid = NET:New():GetPlayerUCID(nil, playerName)
                        responseHandle(ucid, "反辐射", 1, AI_Su34SEAD, unit:getID(), playerName)
                    end, {})
            end
        end
    end
end

world.addEventHandler(e)
